define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        models = require('js/apps/stats/models'),
        opModelCls = require('js/apps/op/models'),
        utils = require('js/utils/tools'),
        chart = require('js/utils/chart'),
        helper = require('js/apps/report/common'),
        moment = require('moment'),
        app = Backbone.history;
    require('magnific');
    require('multiselect');


    var AccountLoginListView = Backbone.View.extend({
        events: {
            'click #search': 'doSearch'
        },
        initialize: function (options) {
            this.options = options;
            this.collection = new models.AccountLoginCollection();
            this.collection.bind('change remove reset', this.renderWithData, this);
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },

        _getSearchOption: function () {
            var options = {};
            var searched = utils.getAllInput('#sidebar_right');
            searched.device_id = $('#searchDeviceId').val();
            searched.register_ip = $("#searchRegisterIp").val();
            searched.last_login_ip = $("#searchLastLoginIp").val();
            if(null != searched.device_id && '' != searched.device_id){
                options.aid = searched.device_id;
            }
            if (null != searched.register_ip && ''!= searched.register_ip) {
                options.ip = searched.register_ip;
            }
            if (null != searched.last_login_ip && ''!= searched.last_login_ip) {
                options.last_login_ip = searched.last_login_ip;
            }

            utils.saveSearched('stats/account_login/', searched);
            return options;
        },

        doSearch: function () {
            var options = this._getSearchOption();
            options.$page = 1;
            options.$size = 30;
            options._ = new Date().getTime();
            app.navigate(utils.composeQueryString('#stats/account_login/', options), {
                trigger: true
            });
        },

        renderWithData: function () {
            this.$el.html(this.template({
                items: this.collection.toJSON(),
                searched: utils.getSearched('stats/account_login/')
            }));
            var that = this;
            utils.renderTable('main-list', {
                $orderby: that.options.$orderby || 'id',
                sortCallback: function (field) {
                    that.options.$orderby = field;
                    that.options.$page = 1;
                    var newUrl = utils.composeQueryString('#stats/account_login/', that.options);
                    app.navigate(newUrl, {
                        trigger: true
                    });
                }
            });
            utils.getPaginator(that.options, this.collection.total, '#stats/account_login/');

            return this;
        },
        load: function () {
            this.collection.fetch({
                reset: true,
                data: this.options,
                error: function (c, r, o) {
                    notify.notifyResp(r);
                    $('#content').append('<h4 class="text-muted">无数据</h4>');
                },
            });
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "stats/AccountLogin.html",
        initialize: function (options) {
            this.options = options;
        },
        render: function () {
            this.options.$size = 20;
            var view = new AccountLoginListView(this.options);
            view.template = this.template;
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();

            return this;
        }
    });
});